package model

import (
	"encoding/json"
	"github.com/jinzhu/copier"
	"gorm.io/gorm"
)

type Question struct {
	gorm.Model
	Title      string `gorm:"type:varchar(255);comment:标题;column:title"`
	Content    string `gorm:"type:text;comment:内容;column:content"`
	Difficulty int    `gorm:"type:int;comment:难度等级;column:difficulty"`
	Answers    string `gorm:"type:text;comment:推荐答案;column:answers"`
	Tags       string `gorm:"type:varchar(255);comment:标签列表（json 数组）;column:tags"`
	UserId     uint   `gorm:"type:bigint;comment:创建用户id;column:user_id"`
}

func (Question) TableName() string {
	return "question"
}

func (Question) TableComment() string {
	return "题目表"
}

type QuestionVo struct {
	Id         uint     `form:"questionId"`
	Title      string   `json:"title"`
	Content    string   `json:"content"`
	Difficulty int      `json:"difficulty"`
	Answers    string   `json:"answers"`
	Tags       []string `json:"tags"`
}

type QuestionQueryParam struct {
	PageRequest
	QuestionId uint     `form:"questionId"`
	Title      string   `form:"title"`
	Content    string   `form:"content"`
	Difficulty int      `form:"difficulty"`
	Answers    string   `form:"answers"`
	Tags       []string `form:"tags"`
	UserId     uint     `form:"userId"`
}

func ConvertToQuestionVo(question Question) (QuestionVo, error) {
	var questionVo QuestionVo
	if err := copier.Copy(&questionVo, &question); err != nil {
		return questionVo, err
	}
	// 复杂字段，手动处理
	if err := json.Unmarshal([]byte(question.Tags), &questionVo.Tags); err != nil {
		return questionVo, err
	}
	return questionVo, nil
}
